<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\7\2 0002
 * Time: 17:53
 */

namespace App\Http\Controllers\Common;


use App\Models\MeetingIssue;
use App\Models\MeetingIssueFile;
use App\Models\MeetingIssueFront;
use App\Models\WebsiteFile;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class DownLoadController extends ApiController
{
    /**
     * 下载文件
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function download(Request $request)
    {
        $input = $request->only(['file_id']);
        $result = Validator::make($input, [
            'file_id' => 'required|integer',
        ], [
            'file_id.required' => '缺少文件参数！',
            'file_id.integer' => '文件参数错误！',
        ]);
        if ($result->fails()) {
            return $this->setStatusCode(412)->message($result->errors()->first());
        }
        $data = WebsiteFile::where('id', $input['file_id'])->first(['title', 'path']);
        $title = $data['title'];
        $path = $data['path'];
        $path = json_decode($path, true);
        if (empty($path) || !isset($path[0]['url']))
            return $this->setStatusCode(412)->message('文件不存在！');
        WebsiteFile::where('id', $input['file_id'])->increment('clicks');
        $file = \base_path() . '/public_html' . $path[0]['url'];
        $suffix = explode('.', $path[0]['url']);
        return response()->download($file, $title . '.' . $suffix[1]);
    }

    /**
     * 下载文件
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function download1(Request $request)
    {
        $input = $request->only(['file_url']);
        $result = Validator::make($input, [
            'file_url' => 'required',
        ], [
            'file_url.required' => '缺少文件路径参数！',
        ]);
        if ($result->fails()) {
            return $this->setStatusCode(412)->message($result->errors()->first());
        }
        if (empty($input['file_url']))
            return $this->setStatusCode(412)->message('文件路径不可为空！');
        $file = \base_path() . '/public_html' . $input['file_url'];
        $suffix = explode('.', $input['file_url']);
        return response()->download($file, '批量增加员工模板' . '.' . $suffix[1]);
    }

    /**
     * 下载议题
     * @param Request $request
     * @return string|\Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function downloadIssue(Request $request)
    {
        $input = $request->only(['id']);
        $result = Validator::make($input, [
            'id' => 'required|integer',
        ], [
            'id.required' => '缺少文件参数！',
            'id.integer' => '文件参数错误！',
        ]);
        if ($result->fails()) {
            return $result->errors()->first();
        }
        $data = MeetingIssue::getFirst($input['id']);
        $title = $data['issue'];
        $path = $data['file_path'];
        if (empty($path))
            return '文件不存在！';
        $file = \base_path() . '/public_html' . $path;
        $suffix = explode('.', $path);
        return response()->download($file, $title . '议题文件.' . $suffix[1]);
    }

    /**
     * 下载议题
     * @param Request $request
     * @return string|\Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function downloadIssueFile(Request $request)
    {
        $input = $request->only(['id']);
        $result = Validator::make($input, [
            'id' => 'required|integer',
        ], [
            'id.required' => '缺少文件参数！',
            'id.integer' => '文件参数错误！',
        ]);
        if ($result->fails()) {
            return $result->errors()->first();
        }
        $data = MeetingIssueFile::getFirst($input['id']);
        $title = $data['name'];
        $path = $data['url'];
        if (empty($path))
            return '文件不存在！';
        $file = \base_path() . '/public_html' . $path;
        $suffix = explode('.', $path);
        return response()->download($file, $title . $suffix[1]);
    }

    /**
     * 下载议题
     * @param Request $request
     * @return string|\Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function downloadIssueFront(Request $request)
    {
        $input = $request->only(['id']);
        $result = Validator::make($input, [
            'id' => 'required|integer',
        ], [
            'id.required' => '缺少文件参数！',
            'id.integer' => '文件参数错误！',
        ]);
        if ($result->fails()) {
            return $result->errors()->first();
        }
        $data = MeetingIssueFront::getFirst($input['id']);
        $title = $data['issue'];
        $path = $data['file_path'];
        if (empty($path))
            return '文件不存在！';
        $file = \base_path() . '/public_html' . $path;
        $suffix = explode('.', $path);
        return response()->download($file, $title . '议题文件.' . $suffix[1]);
    }

}